package com.oyjd.framework.ui.gallery;

import java.util.List;

import android.annotation.SuppressLint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oyjd.R;
import com.oyjd.framework.util.ImageHelp;

/**
 * 3D相册视图类的控制器，负责动态加载图片和响应点击事件及切屏事件
 * 
 * @author dengweiming
 * 
 */
public class GalleryController {

	Gallery mGallery;
	TextView mTitle;
	TextView mDescription;
	RelativeLayout layout;
	boolean isautomove = true;
	ImageView leftbtn;
	ImageView rightbtn;
	RadioGroup rg;
	MyHandler myhandler = new MyHandler();
	List<ImageAndText> mData;
	AsyncImageLoader mAsyncImageLoader = new AsyncImageLoader();
	int mCount;
	int mDoneCount;

	public GalleryController(Gallery gallery) {
		if (gallery == null) {
			throw new IllegalArgumentException("Gallery is null!");
		}
		mGallery = gallery;
		mGallery.setController(this);
	}

	/**
	 * 设置文字视图
	 * 
	 * @param view1
	 * @param view2
	 */
	public void setTextView(TextView title, TextView description) {
		mTitle = title;
		mDescription = description;
	}

	public void setLayout(RelativeLayout layout) {
		this.layout = layout;
	}

	/**
	 * 设置图片地址以及文字等数据的列表，并开始动态加载图片
	 * 
	 * @param data
	 * @param width
	 * @param height
	 */
	public void setData(List<ImageAndText> data) {
		if (mData != null) {
			mData.clear();
		}
		mData = data;
		mCount = mData.size();
		mDoneCount = 0;
		Drawable drawable = mGallery.getResources().getDrawable(R.drawable.gallery_default_image);

		mGallery.setOptimizeClipRect(false);
		mGallery.removeAllViews();
		for (int i = 0; i < mCount; ++i) {
			String url = mData.get(i).getImageUrl();
			ImageView imageView = new ImageView(mGallery.getContext());
			imageView.setBackgroundColor(0);
			imageView.setScaleType(ScaleType.FIT_CENTER);
			imageView.setImageDrawable(drawable);
			imageView.setTag(url);
			mGallery.addView(imageView);
			ImageHelp.doImg(imageView, url);
//			mAsyncImageLoader.loadDrawable(url, new AsyncImageLoader.ImageCallback() {
//				public void imageLoaded(Drawable imageDrawable, String imageUrl) {
//					ImageView imageView = (ImageView) mGallery.findViewWithTag(imageUrl);
//					if (imageView != null && imageDrawable != null) {
//						imageView.setImageDrawable(imageDrawable);
//						mGallery.postInvalidate();
//						if (++mDoneCount >= mCount) {
//							// 图片全部加载完成时假设它们的大小都相等，可以优化裁剪，提高绘制效率
//							mGallery.setOptimizeClipRect(true);
//						}
//					}
//				}
//			});
		}
		initScrollBar();
	}

	@SuppressWarnings("deprecation")
	void initScrollBar() {
		if (mCount > 0) {

			rg = new RadioGroup(mGallery.getContext());
			rg.setOrientation(LinearLayout.HORIZONTAL);
			rg.setBackgroundResource(R.drawable.radiogroupbg);
			rg.setGravity(Gravity.CENTER_HORIZONTAL);
			RelativeLayout.LayoutParams rgly = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
			rgly.setMargins(0, 0, 0, 20);
			rgly.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
			rgly.addRule(RelativeLayout.CENTER_HORIZONTAL);
			rg.setLayoutParams(rgly);
			for (int i = 0; i < mCount; i++) {
				RadioButton rb = new RadioButton(mGallery.getContext());
				rb.setBackgroundResource(R.drawable.radiobtncenterdraw);
				rb.setGravity(Gravity.CENTER);
				rb.setClickable(false);
				rb.setId(i);

				rb.setPadding(25, 0, 25, 0);
				rb.setButtonDrawable(new BitmapDrawable() {
				});
				rb.setText(String.valueOf(i + 1));
				RadioGroup.LayoutParams rbly = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
				rb.setLayoutParams(rbly);
				rg.addView(rb);
			}

			rg.check(0);

			layout.addView(rg);
//			leftbtn = (ImageView) layout.findViewById(R.id.leftbtn);
//			leftbtn.setOnClickListener(new OnClickListener() {
//
//				public void onClick(View v) {
//					// TODO Auto-generated method stub
//					int currentScreen = mGallery.mCurrentScreen;
//					if (!(currentScreen + 1 >= mCount)) {
//						mGallery.flingToScreen(currentScreen + 1, 300);
//					}
//
//				}
//
//			});
//			rightbtn = (ImageView) layout.findViewById(R.id.rightbtn);
//			rightbtn.setOnClickListener(new OnClickListener() {
//
//				public void onClick(View v) {
//					// TODO Auto-generated method stub
//					int currentScreen = mGallery.mCurrentScreen;
//					if (currentScreen - 1 >= 0) {
//						mGallery.flingToScreen(currentScreen - 1, 500);
//					}
//
//				}
//
//			});
			final Thread thread = new Thread(new Runnable() {

				public void run() {
					// TODO Auto-generated method stub
					while (true && isautomove) {

						try {
							Thread.sleep(5000);
							if (isautomove) {
								myhandler.sendMessage(new Message());
							}

						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				}
			});
			thread.start();
		}

	}

	/**
	 * 响应点击事件
	 * 
	 * @param screen
	 *            点击的图片的索引
	 */
	void onClick(int screen) {
		
	}

	/**
	 * 响应滑动停止的事件，一般不用处理
	 * 
	 * @param screen
	 *            当前图片的索引
	 */
	void onScrollFinish(int screen) {
	}

	/**
	 * 滑动到一半触发的事件，切换文字
	 * 
	 * @param newScreen
	 *            当前图片的索引
	 * @param oldScreen
	 *            之前图片的索引
	 */
	void onScreenChanged(int newScreen, int oldScreen) {
		if (0 <= newScreen && newScreen < mCount) {
//			ImageAndText item = mData.get(newScreen);
			//mTitle.setText(item.getTitle());
			rg.check(newScreen);
//			if (newScreen == 0) {
//				rightbtn.setVisibility(View.INVISIBLE);
//			} else if (newScreen == mCount - 1) {
//				leftbtn.setVisibility(View.INVISIBLE);
//			} else {
//				leftbtn.setVisibility(View.VISIBLE);
//				rightbtn.setVisibility(View.VISIBLE);
//			}
			// mDescription.setText(item.getDescription());
		}
	}

	@SuppressLint("HandlerLeak")
	private class MyHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			int currentScreen = mGallery.mCurrentScreen;
			if (!(currentScreen + 1 >= mCount)) {
				mGallery.flingToScreen(currentScreen + 1, 300);
			} else {
				mGallery.flingToScreen(0, 300);
			}

		}

	}

}